home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************************
- #
- # scanDialog.c
- #
- # This segment contains dialog handling functions for scanning.
- # (not neccessary functions)
- #
- # Author(s): Michael Marinkovich
- # Apple Developer Technical Support
- # marink@apple.com
- #
- # Modification History:
- #
- # 2/10/96 MWM Initial coding
- #
- # Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
- #
- #
- # You may incorporate this sample code into your applications without
- # restriction, though the sample code has been provided "AS IS" and the
- # responsibility for its operation is 100% yours. However, what you are
- # not permitted to do is to redistribute the source as "DSC Sample Code"
- # after having made changes. If you're going to re-distribute the source,
- # we require that you make it clear in the source that the code was
- # descended from Apple Sample Code, but that you've made changes.
- #
- *************************************************************************************/
-
- #include <Controls.h>
- #include <Events.h>
- #include <ToolUtils.h>
- #include <Gestalt.h>
- #include <OSUtils.h>
- #include <Palettes.h>
-
- #include "App.h"
- #include "Proto.h"
-
-
- //----------------------------------------------------------------------
- //
- // SetupAndScan - Entry point for scan sample
- //
- //
- //----------------------------------------------------------------------
-
- void SetupAndScan(void)
- {
- OSErr err;
- WindowRef window;
- CGrafPtr oldPort;
- GWorldPtr theWorld;
- GDHandle oldGD;
- ScStdFeaturesRec scannerInfo;
- PixMapHandle thePix;
- Rect theRect;
- DocHnd doc;
- Str255 title = "\p";
-
-
- GetGWorld(&oldPort, &oldGD);
-
- err = DoNewScan(&theWorld);
-
- SetGWorld(oldPort, oldGD);
-
- if (err == noErr && theWorld != nil)
- {
-
- window = CreateWindow(128, nil, nil, title, true, documentProc,
- kDocKind, (WindowPtr)-1,true, nil );
-
- doc = (DocHnd)GetWRefCon(window);
-
- if (doc != nil)
- {
- HLock((Handle)doc);
- SetGWorld((CGrafPtr)window, nil);
-
- (**doc).world = theWorld;
- InvalRect(&(**doc).world->portRect);
- AdjustScrollbars(window, true);
-
- HUnlock((Handle)doc);
-
- }
- }
- else
- // user cancelled - just continue
- if (err == userCanceledErr)
- err = noErr;
-
- if (err != noErr)
- HandleError(err, false);
-
- }
-
-
- //----------------------------------------------------------------------
- //
- // GetUserScanInfo - request the depth, resolution, and color
- // requirements of the scan and finally do the scan.
- //
- //----------------------------------------------------------------------
-
- OSErr GetUserScanInfo(CGrafPtr *dstPort, ScStdFeaturesRec *scannerInfo,
- ScResPtr lineArtRes, ScResPtr halfToneRes, ScResPtr grayScaleRes,
- ScResPtr biLevelColorRes, ScResPtr fullColorRes,
- ScHalfTonePtr halfToneNames, UserSettingsPtr userSettings)
- {
- OSErr err = noErr;
- ScTPUADFInfo TPUADFInfo;
- ScStdFeaturesRec newScannerInfo;
- DialogPtr dialog;
- Handle item;
- ModalFilterUPP modalUPP;
- Str255 str;
- Rect box;
- short itemType;
- short resItem;
- short itemHit;
- short selection;
- Boolean done = false;
- Boolean ADF;
- Boolean TPU;
-
-
- SetCursor(&qd.arrow);
-
- if (err == noErr)
- {
- dialog = GetNewDialog(rNewScanDlg, nil, (WindowPtr) -1);
-
- if (dialog != nil)
- {
- ShowWindow(dialog);
- SetPort(dialog);
-
- SetDialogDefaultItem(dialog, ok);
-
- newScannerInfo = *scannerInfo;
-
- err = AdjustScanDialogItems(dialog, newScannerInfo,
- lineArtRes, halfToneRes, grayScaleRes,
- biLevelColorRes, fullColorRes, halfToneNames);
-
- modalUPP = NewModalFilterProc(StdDialogEventFilter);
-
- // loop until user selects OK or CANCEL
- while (!done)
- {
- ModalDialog(modalUPP, &itemHit );
- done = (itemHit == cancel || itemHit == ok);
-
- if (itemHit == kColorPopup || itemHit == kSourcePopup)
- {
- if (itemHit == kSourcePopup)
- {
- err = GetInputMethods(&ADF, &TPU, &TPUADFInfo);
- GetDItem (dialog, kSourcePopup, &itemType, &item, &box);
- selection = GetControlValue((ControlRef)item);
-
- // change rect size according to which input method user has
- // chosen. If it is ADF of TPU then get the width and height
- // from the TPUADFInfo structure obtained from GetInputMethods.
- switch (selection)
- {
- case kSourceFlatbedItem:
- newScannerInfo = *scannerInfo;
- break;
-
- case kSourceADFItem:
- newScannerInfo.scanWidthNum = TPUADFInfo.ADFScanWidthNum;
- newScannerInfo.scanWidthDen = TPUADFInfo.ADFScanWidthDen;
- newScannerInfo.scanLengthNum = TPUADFInfo.ADFScanLengthNum;
- newScannerInfo.scanLengthDen = TPUADFInfo.ADFScanLengthDen;
- break;
-
- case kSourceTPUItem:
- newScannerInfo.scanWidthNum = TPUADFInfo.TPUScanWidthNum;
- newScannerInfo.scanWidthDen = TPUADFInfo.TPUScanWidthDen;
- newScannerInfo.scanLengthNum = TPUADFInfo.TPUScanLengthNum;
- newScannerInfo.scanLengthDen = TPUADFInfo.TPUScanLengthDen;
- break;
- }
- }
-
- err = AdjustScanDialogItems(dialog, newScannerInfo,
- lineArtRes, halfToneRes, grayScaleRes,
- biLevelColorRes, fullColorRes, halfToneNames);
-
- }
- }
-
- if (itemHit == ok)
- {
- // set input method
- GetDItem (dialog, kSourcePopup, &itemType, &item, &box);
-
- userSettings->inputMethod = GetControlValue((ControlRef)item);
-
- // if user selected TPU we need to get the transparency options
- if (userSettings->inputMethod == kSourceTPUItem)
- {
- err = GetUserTPUSettings(&userSettings->TPUFilm, &userSettings->TPUVendor);
- userSettings->TPULight = true;
- }
- else
- {
- // set the TPU settings to false if we are using the flatbed
- userSettings->TPUFilm = 0;
- userSettings->TPULight = 0;
- userSettings->TPUVendor = 0;
- }
-
- // set compMode...color, grayscale, etc..
- GetDItem (dialog, kColorPopup, &itemType, &item, &box);
-
- selection = GetControlValue((ControlRef)item) - 1;
-
- // adjust final value to reflect the lack of a Bi-Level popup item
- if (selection == scBiLevelColor)
- selection++;
-
- userSettings->compMode = selection;
-
- // set scan resolution
- GetDItem (dialog, kResolutionPopup, &itemType, &item, &box);
- resItem = GetControlValue((ControlRef)item);
-
- userSettings->resolution = GetCompModeResolution(userSettings->compMode, resItem - 1,
- lineArtRes, halfToneRes, grayScaleRes,
- biLevelColorRes, fullColorRes);
-
- // get height and width from dialog items
- GetDItem (dialog, kHeightItem, &itemType, &item, &box);
- if (item != nil)
- {
- GetDialogItemText(item, str);
- StringToNum(str, &userSettings->height);
- }
-
- GetDItem (dialog, kWidthItem, &itemType, &item, &box);
- if (item != nil)
- {
- GetDialogItemText(item, str);
- StringToNum(str, &userSettings->width);
- }
-
- // just set these items to middle ground
- userSettings->brightness = 128;
- userSettings->contrast = 128;
- userSettings->ditherPattern = 0;
-
- // make sure brightness & contrast are not greater than max values
- if (userSettings->brightness > scannerInfo->composition[userSettings->compMode].brightnessMax)
- userSettings->brightness = scannerInfo->composition[userSettings->compMode].brightnessMax;
-
- if (userSettings->contrast > scannerInfo->composition[userSettings->compMode].contrastMax)
- userSettings->contrast = scannerInfo->composition[userSettings->compMode].contrastMax;
-
- // if user selected HalfTone we need to get the pattern
- if (userSettings->compMode == scHalfTone)
- err = GetUserHalfTone(&userSettings->ditherPattern, halfToneNames, *scannerInfo);
-
-
- }
- // user hit cancel - make sure we didn't already have an err pending.
- else
- if (err == noErr) err = userCanceledErr;
-
- DisposeRoutineDescriptor(modalUPP);
- DisposeDialog(dialog);
-
- }
-
- }
-
- return err;
-
- }
-
-
-
- //----------------------------------------------------------------------
- //
- // AdjustScanDialogItems - Get scanner capabilities and enable/disable
- // dialog items accordingly.
- //
- //----------------------------------------------------------------------
-
- OSErr AdjustScanDialogItems(DialogRef dialog, ScStdFeaturesRec scannerInfo,
- ScResPtr lineArtRes, ScResPtr halfToneRes,
- ScResPtr grayScaleRes, ScResPtr biLevelColorRes,
- ScResPtr fullColorRes, ScHalfTonePtr halfToneNames)
- {
- OSErr err = noErr;
- Handle item;
- MenuRef menu;
- PopupPrivateDataHandle popupData;
- ScTPUADFInfo TPUADFInfo;
- Rect box;
- Str255 str;
- short res;
- short itemType;
- short menuItems;
- short value;
- short i;
- Boolean ADF;
- Boolean TPU;
-
- // source input method
- GetDItem (dialog, kSourcePopup, &itemType, &item, &box);
- if (item != nil)
- {
- popupData = (PopupPrivateDataHandle)(**(ControlRef)item).contrlData;
- if (popupData != nil)
- menu = (**popupData).mHandle;
-
- if (menu != nil)
- {
- // disable ADF first
- DisableItem(menu, kSourceADFItem);
- DisableItem(menu, kSourceTPUItem);
-
- err = GetInputMethods(&ADF, &TPU, &TPUADFInfo);
- if (err == noErr)
- {
- if (ADF)
- EnableItem(menu, kSourceADFItem);
- else
- {
- if (TPU)
- EnableItem(menu, kSourceTPUItem);
- }
-
- }
- else
- err = noErr;
- }
-
- }
-
- // get current compMode method from popup
- GetDItem (dialog, kColorPopup, &itemType, &item, &box) ;
- if (item)
- value = GetControlValue((ControlRef)item);
-
- // get popup for resolutions
- GetDItem (dialog, kResolutionPopup, &itemType, &item, &box) ;
- if (item)
- {
- popupData = (PopupPrivateDataHandle)(**(ControlRef)item).contrlData;
- menu = (**popupData).mHandle ;
-
- menuItems = CountMItems(menu);
- DeleteMenuItem(menu, 0);
-
- // clear items from menu so we can rebuild
- for (i = 0; i < menuItems + 1; i++)
- DeleteMenuItem(menu, 1);
-
- // subtract one to access zero based array
- value--;
-
- // since there is not a BiLevelColor item in the list increment
- // the value to indicate that we actually wanted color
- if (value == scBiLevelColor)
- value++;
-
- menuItems = scannerInfo.composition[value].resElements;
-
- // fill menu with new resolutions
- for (i = 0; i < menuItems; i++)
- {
- res = GetCompModeResolution(value, i, lineArtRes,
- halfToneRes, grayScaleRes,
- biLevelColorRes, fullColorRes);
-
- NumToString(res, str);
- if (i == 0)
- InsertMenuItem(menu, str, 0);
- else
- InsertMenuItem(menu, str, i);
- }
-
- SetControlValue((ControlRef)item, 1);
- InvalRect(&box);
-
- }
-
- // set height text item
- GetDialogItem(dialog, kHeightItem, &itemType, &item, &box);
- NumToString((72L * scannerInfo.scanLengthNum) / scannerInfo.scanLengthDen, str);
- SetDialogItemText(item, str);
-
- // set width text item
- GetDialogItem(dialog, kWidthItem, &itemType, &item, &box);
- NumToString((72L * scannerInfo.scanWidthNum) / scannerInfo.scanWidthDen, str);
- SetDialogItemText(item, str);
-
- SelectDialogItemText(dialog, kHeightItem, 0, 32767);
-
- return err;
-
- }
-
-
-
- //----------------------------------------------------------------------
- //
- // GetCompModeResolution - get resolution from resPtr specified
- // by compMode.
- //
- //----------------------------------------------------------------------
-
- long GetCompModeResolution(short compMode, short item, ScResPtr lineArtRes,
- ScResPtr halfToneRes, ScResPtr grayScaleRes,
- ScResPtr biLevelColorRes, ScResPtr fullColorRes)
- {
- long res;
-
- switch (compMode)
- {
- case scLineArt:
- res = *(lineArtRes[item]);
- break;
-
- case scHalfTone:
- res = *(halfToneRes[item]);
- break;
-
- case scGrayScale:
- res = *(grayScaleRes[item]);
- break;
-
- case scBiLevelColor:
- res = *(biLevelColorRes[item]);
- break;
-
- case scFullColor:
- res = *(fullColorRes[item]);
- break;
-
- default:
- res = 0;
- break;
- }
-
- return res;
-
- }
-
-
- //----------------------------------------------------------------------
- //
- // GetUserHalfTone - ask user what halftone to use
- //
- //
- //----------------------------------------------------------------------
-
- OSErr GetUserHalfTone(short *selection, ScHalfTonePtr halfToneNames,
- ScStdFeaturesRec scannerInfo)
- {
- OSErr err = noErr;
- DialogPtr dialog;
- ModalFilterUPP modalUPP;
- Handle item;
- Rect box;
- short itemType;
- short resItem;
- short itemHit;
- Str255 str;
- Boolean done = false;
-
- dialog = GetNewDialog(rHalfToneDlg, nil, (WindowPtr) -1);
-
- if (dialog != nil)
- {
- ShowWindow(dialog);
- SetPort(dialog);
-
- SetDialogDefaultItem(dialog, ok);
-
- err = AdjustHalfToneItems(dialog, halfToneNames, scannerInfo);
-
- modalUPP = NewModalFilterProc(StdDialogEventFilter);
-
- while (!done)
- {
- ModalDialog(modalUPP, &itemHit);
- done = (itemHit == cancel || itemHit == ok);
-
- }
-
- if (itemHit == ok)
- {
- // set HalfTone Pattern
- GetDItem (dialog, kHalfTonePopup, &itemType, &item, &box);;
- *selection = GetControlValue((ControlRef)item);
- }
-
- // user hit cancel - make sure we didn't already have an err pending.
- else
- if (err == noErr) err = userCanceledErr;
-
- DisposeRoutineDescriptor(modalUPP);
- DisposeDialog(dialog);
-
- }
-
-
- return err;
-
- }
-
-
- //----------------------------------------------------------------------
- //
- // AdjustHalfToneItems - adjust HalfTone names in popup
- //
- //
- //----------------------------------------------------------------------
-
- OSErr AdjustHalfToneItems(DialogRef dialog, ScHalfTonePtr halfToneNames,
- ScStdFeaturesRec scannerInfo)
- {
- OSErr err = noErr;
- Handle item;
- MenuRef menu;
- PopupPrivateDataHandle popupData;
- Rect box;
- Str255 str;
- short res;
- short itemType;
- short menuItems;
- short i;
-
-
- // get popup for HalfTones
- GetDItem (dialog, kHalfTonePopup, &itemType, &item, &box) ;
- if (item)
- {
- popupData = (PopupPrivateDataHandle)(**(ControlRef)item).contrlData;
- menu = (**popupData).mHandle ;
-
- menuItems = CountMItems(menu);
- DeleteMenuItem(menu, 0);
-
- // clear items from menu so we can rebuild
- for (i = 0; i < menuItems + 1; i++)
- DeleteMenuItem(menu, 1);
-
- menuItems = scannerInfo.composition[scHalfTone].halfToneElements;
-
- // fill menu with new resolutions
- for (i = 0; i < menuItems; i++)
- {
-
- pstrcpy(str, (unsigned char *)halfToneNames[i]);
-
- if (i == 0)
- InsertMenuItem(menu, str, 0);
- else
- InsertMenuItem(menu, str, i);
- }
-
- SetControlValue((ControlRef)item, 1);
- InvalRect(&box);
-
- }
-
- return err;
-
- }
-
-
- //----------------------------------------------------------------------
- //
- // GetUserTPUSettings - ask the user for transparency settings.
- //
- //
- //----------------------------------------------------------------------
-
- OSErr GetUserTPUSettings(short *TPUFilm, short *TPUVendor)
- {
- OSErr err = noErr;
- DialogPtr dialog;
- ModalFilterUPP modalUPP;
- Handle item;
- Rect box;
- short itemType;
- short resItem;
- short itemHit;
- short temp;
- Str255 str;
- Boolean done = false;
-
- dialog = GetNewDialog(rTransparentDlg, nil, (WindowPtr) -1);
-
- if (dialog != nil)
- {
- ShowWindow(dialog);
- SetPort(dialog);
-
- SetDialogDefaultItem(dialog, ok);
-
- SetRadioValue(dialog, kTPUPositive, 1);
- SetRadioValue(dialog, kTPUNegative, 0);
-
- modalUPP = NewModalFilterProc(StdDialogEventFilter);
-
- while (!done)
- {
- ModalDialog(modalUPP, &itemHit);
- done = (itemHit == cancel || itemHit == ok);
-
- if (itemHit == kTPUPositive || itemHit == kTPUNegative)
- {
- SetNotRadioValue(dialog, kTPUPositive);
- SetNotRadioValue(dialog, kTPUNegative);
- }
-
- }
-
- if (itemHit == ok)
- {
- // set TPU NP setting
- GetDItem(dialog, kTPUPositive, &itemType, &item, &box);
- *TPUFilm = !GetControlValue((ControlRef)item);
-
- // set film vendor
- GetDItem(dialog, kTPUFilmPopup, &itemType, &item, &box);
- *TPUVendor = GetControlValue((ControlRef)item) - 1;
- }
-
- // user hit cancel - make sure we didn't already have an err pending.
- else
- if (err == noErr) err = userCanceledErr;
-
- DisposeRoutineDescriptor(modalUPP);
- DisposeDialog(dialog);
-
- }
-
-
- return err;
-
- }
-
-
- //----------------------------------------------------------------------
- //
- // StdDialogEventFilter -
- //
- //
- //----------------------------------------------------------------------
-
- pascal Boolean StdDialogEventFilter(DialogPtr dialog, EventRecord *event, short *itemHit)
- {
- OSErr err;
- CGrafPtr oldPort;
- GDHandle oldGD;
- WindowRef window;
- ControlHandle slider = nil;
- ModalFilterUPP modalProc;
- Handle item;
- Rect itemRect;
- short itemType;
- short key;
- short thePart;
- short i;
- Boolean accepted = false;
-
-
- GetGWorld(&oldPort, &oldGD);
- SetGWorld((CGrafPtr)dialog, nil);
-
- switch(event->what)
- {
- case keyDown:
- case autoKey:
- key = event->message & charCodeMask;
- accepted = false;
- break;
-
- case updateEvt:
- if (event->message != (long)dialog) // in case a screen saver activates.
- {
- CustomWindowEvent(kUpdateProc,(WindowPtr)event->message,nil);
- SetCursor(&qd.arrow);
- }
-
- if (event->message == (long)dialog) // in case a screen saver activates.
- {
- UpdateDialog(dialog, dialog->visRgn);
- SetCursor(&qd.arrow);
-
- }
- break;
-
- case mouseDown:
- thePart = FindWindow(event->where,&window);
-
- switch(thePart)
- {
- case inContent:
- break;
-
- case inSysWindow:
- SysBeep(30);
- break;
-
- case inDrag:
- if (window == dialog)
- {
- DragWindow(dialog, event->where, &qd.screenBits.bounds);
- accepted = true;
- }
- break;
- }
- break;
-
- }
-
- if (!accepted)
- {
- err = GetStdFilterProc(&modalProc);
- if (err == noErr)
- {
- accepted = CallModalFilterProc(modalProc, dialog, event, itemHit);
- DisposeRoutineDescriptor(modalProc);
- }
- }
-
- SetGWorld(oldPort, oldGD);
-
- return(accepted);
-
- }
-
-
- //----------------------------------------------------------------------
- //
- // SetRadioValue -
- //
- //
- //----------------------------------------------------------------------
-
- void SetRadioValue(DialogRef dialog, short whichItem, short value)
- {
- Handle item;
- Rect box;
- short itemType;
-
- GetDItem (dialog, whichItem, &itemType, &item, &box);
- SetControlValue((ControlRef)item, value);
-
-
- }
-
- //----------------------------------------------------------------------
- //
- // SetNotRadioValue -
- //
- //
- //----------------------------------------------------------------------
-
- void SetNotRadioValue(DialogRef dialog, short whichItem)
- {
- Handle item;
- Rect box;
- short itemType;
- short value;
-
- GetDItem (dialog, whichItem, &itemType, &item, &box);
- value = GetControlValue((ControlRef)item);
-
- SetControlValue((ControlRef)item, !value);
-
-
- }
-